package com.winchain.chainbill.bill.biz.contract.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractSponsorCmd;
import com.winchain.chainbill.bill.biz.transfer.vo.BillXfe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ContractSponsorSubscriber<T> extends OnErrorNotImplementedSubscriber<BillEvent<T>> {
	@Autowired
	private ContractSponsorCmd<T> contractSponsorCmd;

	public void onApplicationEvent(BillEvent<T> billEvent) {
		Bill<T> bill = billEvent.getBill();
		CommandContext<Bill<T>> context = new CommandContext();
		context.setSource(bill);
		if ((bill instanceof BillXfe)) {
			BillXfe billXfe = (BillXfe) bill;
			context.setValue("Deferral", billXfe.getDfrl());
		}
		ChainBase<Bill<T>> crb = new ChainBase();
		crb.add(this.contractSponsorCmd);
		crb.execute(context);
	}
}
